$(function(){
	/*** 初始化专业树 */
	$('#subject_tree').tree({
	    url : 'subject/subjects.shtml',
	    onClick : function (node) {
	    	//console.log(node);
	    	/*$('#dg').treegrid('reload', {
	    		subjectId : node.id
	    	});*/
	    	$('#dg').datagrid('reload', {
	    		subjectId : node.id
	    	});
	    }
	    });
	
	/** 附件列表 */
	/*$('#dg').datagrid({    
	    //title:"任务反馈列表",
		border:0,
		nowrap:true,
		striped:true,
		fit: true,//自动大小
		url:'chapter/queryBySubjectId.shtml',
		//url:'resource/queryBySubjectId.shtml',
//		queryParams : {bizId : $('#biz_id').val()},
		frozenColumns:[[    
	        {field : 'att_id', title : 'att_id', hidden:true},    
	        {field : 'file_name', title : '文件名称', width:300,
	        	formatter : function (value, row, index) {
	        		//return '<a href="' + contextPath + '/attachment/down?attId=' + row.att_id + '" target="_blank">' + value + '</a>';
//	        		return '<a href="/proms/attachment/down?attId=' + row.att_id + '" target="_blank">' + value + '</a>';
	        		//<a target="_blank" href="/proms/attachment/down?attId=71">Chrysanthemum.jpg</a>
	        		return dealClickAtta(row.att_id, value);
	        	}
	        }
	    ]],
	    columns : [[
			{field : 'title', title : '章名', width : 150},
			{field : 'name', title : '学科名称', width : 200},
			{field : 'remark', title : '备注', width : 200},
			{field : 'createTime', title : '创建时间', width:150}
        ]],
		singleSelect:true,
		rownumbers:true,
		toolbar:'#tb',
		pagination:true,
		pageSize:20,
		onClickRow:function(rowIndex, rowData){}
	}).datagrid('getPager').pagination({  
        pageSize: 20,//每页显示的记录条数，默认为10  
        pageList: [20,50,100],//可以设置每页记录条数的列表  
        beforePageText: '第',//页数文本框前显示的汉字  
        afterPageText: '页    共 {pages} 页',  
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
    });*/
	
	/** 新增 */
	$('#add').click(function() {
		/*$('#form').form('clear');
		$('#win').window('open');*/
		var url = 'chapter/add.shtml';
		var node = $('#subject_tree').tree('getSelected');
		if (null != node) {
			url = url + '?subjectId=' + node.id;
		}
		showEasyUiWindow('contentFrame', url, 'win', '新增学科章', 625, 350);
	});
	
	/** 编辑 */
	$('#edit').click(function() {
		var row = $('#dg').datagrid('getSelected');
		if (null == row) {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		} else {
			var url = 'chapter/edit.shtml?id=' + row.id;
			showEasyUiWindow('contentFrame', url, 'win', '编辑章', 625, 350);
		}
	});
	/*** 删除 */
	$('#remove').click(function(){
		var row = $('#dg').datagrid('getSelected');
		if (row) {
			$.messager.confirm('确认', '你确认要删除此条数据吗?', function(r) {
				if (r) {
					var id = row.id;				
					$.get('chapter/delete.shtml', {id : id}, function(rs) {
						if (rs.success == true) {
							reSearch();
							$.messager.alert('warning', rs.message, 'warning');
						} else {
							$.messager.alert('warning', rs.message, 'warning');
						}
					});
				}
			});
		} else {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		}
	});
});

/*** 重载页面 */
function reSearch() {
	$('#name').val('');
	doSearch();
}
/*** 搜索 */
function doSearch() {
	$('#dg').datagrid('reload', {
		name : $('#name').val()
	});
}
/**
 * 将long型转成date型
 * @param value
 * @param row
 * @returns
 */
function formatterDate(value, row) {
	return formatDate(value, 'yyyy-MM-dd hh:mm:ss');
}

function dealClickAtta(attId, fileName) {
	if(isVideo(fileName)) {
		return '<a href="javascript:void(0);" onclick="playVideo(\'' + attId + '\')">' + fileName + '</a>';
	} else {
		return '<a href="' + contextPath + '/attachment/down?attId=' + attId + '" target="_blank">' + fileName + '</a>';
	}
	
}

function isVideo(fileName) {
	var videoStr = "mp4";
	var suffix = getFileSuffix(fileName);
	if(videoStr.indexOf(suffix)<0){
		suffix = "epub";
	} 
	if(videoStr.indexOf(suffix)<0){
		return false;
	}
	return true;
}

function getFileSuffix(fileName){ 
	if(typeof fileName === 'undefined'){
		return '';
	}else{
		var len = fileName.lastIndexOf('.'); 
		return fileName.substr(len+1,fileName.length);
	}
} 

function playVideo(attId) {
	showEasyUiWindow('contentFrame', 'video/listByAttId.shtml?attId=' + attId, 'win', '视频播放', 640, 380);
}